package cn.kgc.disk.aop;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

@Aspect
public class UserLogAspect {
    //添加日志信息
    private Logger logger = Logger.getLogger(UserLogAspect.class);

    //@Pointcut("execution(public void save(cn.kgc.disk.entity.User))")cn.kgc.disk.entity.User
    //*代表任意，“..”代表所有（）括号内的是方法的参数类型
    @Pointcut("execution(public * cn.kgc.disk.Dao.UserDapImpl.*(..))")
    public void pointCut() {
    }

    @Before("pointCut()")
    public void before(JoinPoint joinPoint) {
        logger.info("调用" + joinPoint.getTarget() + "的" + joinPoint.getSignature() + "方法，参数为：" + joinPoint.getArgs());
    }

    @AfterReturning(pointcut = "pointCut()", returning = "result")
    public void afterReturn(JoinPoint joinPoint, Object result) {
        logger.info("调用" + joinPoint.getTarget() + "的" + joinPoint.getSignature() + "方法，返回值为：" + result);
    }

    @AfterThrowing(pointcut = "pointCut()", throwing = "e")
    public void throwing(JoinPoint joinPoint, RuntimeException e) {
        logger.error(joinPoint.getSignature().getName() + "发生异常" + e);
    }
    @After("pointCut()")
    public void after(JoinPoint joinPoint) {
        logger.info("最终增强，类似于finally" + joinPoint.getSignature());
    }
    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) {
        Object result;
        try {
            result = joinPoint.proceed();
            return result;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }
}
